package leeon.kaixin.wap.action;

import java.util.ArrayList;
import java.util.List;

import leeon.kaixin.wap.util.HttpUtil;
import leeon.mobile.BBSBrowser.ContentException;
import leeon.mobile.BBSBrowser.NetworkException;
import leeon.mobile.BBSBrowser.utils.HTMLUtil;
import leeon.mobile.BBSBrowser.utils.HTTPUtil;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;

public class LoginAction {
	
	public static String home() throws NetworkException {
		HttpClient client = HttpUtil.newInstance();
		HttpGet get = new HttpGet(HttpUtil.KAIXIN_URL);
		
		HttpUtil.setHeader(get);
		
		try {
			HttpResponse response = client.execute(get);
			if (HTTPUtil.isHttp200(response)) {
				String ret = HTTPUtil.toString(response.getEntity());
				ret = HTMLUtil.findStr(ret, HttpUtil.KAIXIN_LOGIN_URL, "\"");
				ret = ret.replace("&amp;", "&");
				return ret;
			} else 
				throw new NetworkException();
		} catch (Exception e) {
			e.printStackTrace();
			throw new NetworkException(e);
		}
	}
	
	public static String login(String user, String password) throws ContentException, NetworkException {
		return login(user, password, null);
	}
	
	public static String login(String user, String password, String code) throws ContentException, NetworkException {
		HttpClient client = HttpUtil.newInstance();
		HttpPost post = new HttpPost(HttpUtil.KAIXIN_LOGIN_URL + (code==null?"":code));
		HttpResponse response = null;
		
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("email", user));
		nvps.add(new BasicNameValuePair("password", password));
		nvps.add(new BasicNameValuePair("login", "登录"));
//		nvps.add(new BasicNameValuePair("from", ""));
//		nvps.add(new BasicNameValuePair("refuid", "0"));
//		nvps.add(new BasicNameValuePair("refcode", ""));
//		nvps.add(new BasicNameValuePair("bind", ""));
//		nvps.add(new BasicNameValuePair("gotourl", ""));
//		nvps.add(new BasicNameValuePair("from_kx_client", "0"));


//		HTTP/1.1 200 OK
//		Date: Mon, 21 Mar 2011 07:17:58 GMT
//		Server: Apache
//		Set-Cookie: _email=leeon.liu%40gmail.com; expires=Tue, 20-Mar-2012 07:17:58 GMT; path=/; domain=.kaixin001.com
//		Set-Cookie: _user=8df20e7b8ca2208f1844e138a5e6f561_6865223_1300691878; path=/; domain=.kaixin001.com
//		Set-Cookie: _kxt=0; expires=Tue, 20-Mar-2012 07:17:58 GMT; path=/; domain=.kaixin001.com
//		Set-Cookie: _uid=6865223; expires=Tue, 22-Mar-2011 07:17:58 GMT; path=/; domain=.kaixin001.com
//		Set-Cookie: _invisible=0; path=/; domain=.kaixin001.com
//		Cache-Control: no-cache
//		Pragma: no-cache
//		Vary: Accept-Encoding,User-Agent
//		Content-Encoding: gzip
//		Content-Length: 5522
//		Connection: close
//		Content-Type: application/xhtml+xml; charset=UTF-8
		HttpUtil.setHeader(post);
		try {
			post.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
			response = client.execute(post);
		} catch (Exception e) {
			e.printStackTrace();
			throw new NetworkException(e);
		}
		
		if (HTTPUtil.isHttp200(response)) {
			String ret = HTTPUtil.toString(response.getEntity());
			ret = HTMLUtil.findStrRegex(ret, "verify=", "[^\\w]");
			if (ret == null) throw new ContentException("账户或者密码不正确");
			return ret;
		} else if (HTTPUtil.isHttp302(response)) {
			throw new ContentException("账户或者密码不正确");
		} else {
			throw new NetworkException();
		}
	}
	
	
	public static String login4www(String user, String password) throws ContentException, NetworkException {
		HttpClient client = HttpUtil.newInstance();
		HttpPost post = new HttpPost(HttpUtil.KAIXIN_WWW_LOGIN_URL);
		HttpResponse response = null;
		
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("email", user));
		nvps.add(new BasicNameValuePair("password", password));

//		HTTP/1.1 302 Found
//		Date: Thu, 24 Mar 2011 05:11:28 GMT
//		Server: Apache
//		Set-Cookie: _ref=9a77dc84ea9fd6fc648cb01cebd5f632..1856.4d8ad2803779d; path=/; domain=.kaixin001.com
//		Set-Cookie: _user=6d1c05c119dc26172ea4d707488194e0_6865223_1300943488; path=/; domain=.kaixin001.com
//		Set-Cookie: _kxt=0; expires=Fri, 23-Mar-2012 05:11:28 GMT; path=/; domain=.kaixin001.com
//		Set-Cookie: _uid=6865223; expires=Fri, 23-Mar-2012 05:11:28 GMT; path=/; domain=.kaixin001.com
//		Set-Cookie: _email=leeon.liu%40gmail.com; expires=Fri, 23-Mar-2012 05:11:28 GMT; path=/; domain=.kaixin001.com
//		Set-Cookie: _invisible=0; path=/; domain=.kaixin001.com
//		Location: /home/?uid=6865223
//		Vary: Accept-Encoding,User-Agent
//		Content-Encoding: gzip
//		Content-Length: 20
//		Connection: close
//		Content-Type: text/html
//		Set-Cookie: SERVERID=_srv101-129_; path=/

		HttpUtil.setHeader(post);
		try {
			post.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
			response = client.execute(post);
			HTTPUtil.consume(response.getEntity());
		} catch (Exception e) {
			e.printStackTrace();
			throw new NetworkException(e);
		}
		
		if (HTTPUtil.isHttp200(response)) {
			throw new ContentException("账户或者密码不正确");
		} else if (HTTPUtil.isHttp302(response)) {
			Header h = response.getFirstHeader("Location");
			if (h != null)
				return h.getValue();
			else
				throw new NetworkException();
		} else {
			throw new NetworkException();
		}
	}
	
	
	public static void home4www(String location) throws NetworkException {
		HttpClient client = HttpUtil.newInstance();
		HttpGet get = new HttpGet(HttpUtil.KAIXIN_WWW_URL + location);
		
		HttpUtil.setHeader(get);
		try {
			HttpResponse response = client.execute(get);
			HTTPUtil.consume(response.getEntity());
		} catch (Exception e) {
			e.printStackTrace();
			throw new NetworkException(e);
		}
	}
	
	public static void logout(String verify) throws NetworkException {
		HttpClient client = HttpUtil.newInstance();
		HttpGet get = new HttpGet(HttpUtil.KAIXIN_LOGOUT_URL + HttpUtil.KAIXIN_PARAM_VERIFY + verify);

//		HTTP/1.1 200 OK
//		Date: Tue, 22 Mar 2011 06:00:44 GMT
//		Server: Apache
//		Set-Cookie: _user=deleted; expires=Mon, 22-Mar-2010 06:00:43 GMT; path=/; domain=.kaixin001.com
//		Set-Cookie: _uid=deleted; expires=Mon, 22-Mar-2010 06:00:43 GMT; path=/; domain=.kaixin001.com
//		Set-Cookie: _wkx=deleted; expires=Mon, 22-Mar-2010 06:00:43 GMT; path=/; domain=.kaixin001.com
//		Set-Cookie: _user=deleted; expires=Mon, 22-Mar-2010 06:00:43 GMT; path=/; domain=.kaixin001.com
//		Set-Cookie: _invisible=deleted; expires=Mon, 22-Mar-2010 06:00:43 GMT; path=/; domain=.kaixin001.com
//		Cache-Control: no-cache
//		Pragma: no-cache
//		Vary: Accept-Encoding,User-Agent
//		Content-Encoding: gzip
//		Content-Length: 1926
//		Connection: close
//		Content-Type: application/xhtml+xml; charset=UTF-8
		HttpUtil.setHeader(get);
		try {
			HttpResponse response = client.execute(get);
			if (response != null && response.getEntity() != null) {
				HTTPUtil.consume(response.getEntity());
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new NetworkException(e);
		}
	}
	
	public static void logout4www() throws NetworkException {
		HttpClient client = HttpUtil.newInstance();
		HttpGet get = new HttpGet(HttpUtil.KAIXIN_WWW_LOGOUT_URL);

//		HTTP/1.1 302 Found
//		Date: Thu, 24 Mar 2011 04:59:32 GMT
//		Server: Apache
//		Set-Cookie: _user=deleted; expires=Wed, 24-Mar-2010 04:59:31 GMT; path=/; domain=.kaixin001.com
//		Set-Cookie: _laid=0; expires=Fri, 23-Mar-2012 04:59:32 GMT; path=/; domain=.kaixin001.com
//		Set-Cookie: _preemail=leeon.liu%40gmail.com; path=/; domain=.kaixin001.com
//		Set-Cookie: _email=deleted; expires=Wed, 24-Mar-2010 04:59:31 GMT; path=/; domain=.kaixin001.com
//		Set-Cookie: _kx=deleted; expires=Wed, 24-Mar-2010 04:59:31 GMT; path=/; domain=.kaixin001.com
//		Set-Cookie: _invisible=deleted; expires=Wed, 24-Mar-2010 04:59:31 GMT; path=/; domain=.kaixin001.com
//		Set-Cookie: presence=deleted; expires=Wed, 24-Mar-2010 04:59:31 GMT; path=/; domain=.kaixin001.com
//		Set-Cookie: _sso=logout; path=/; domain=.kaixin001.com
//		Location: /?452139644=1697336046
//		Vary: Accept-Encoding,User-Agent
//		Content-Encoding: gzip
//		Content-Length: 20
//		Connection: close
//		Content-Type: text/html
		HttpUtil.setHeader(get);
		try {
			HttpResponse response = client.execute(get);
			if (response != null && response.getEntity() != null) {
				HTTPUtil.consume(response.getEntity());
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new NetworkException(e);
		}
	}
	
	public static String uid(String verify) {
		if (verify == null || verify.length() == 0) return null;
		return verify.substring(0, verify.indexOf("_"));
	}
	
	public static String uicon(String uid) {
		//http://pic1.kaixin001.com.cn/logo/81/14/50_1811464_13.jpg
		//http://pic.kaixin001.com.cn/logo/01/01/50_01_01.jpg
		if (uid == null || uid.length() == 0) return null;
		if (uid.length() == 7) {
			String s1 = uid.substring(1, 3);
			String s2 = uid.substring(3, 5);
			return "http://pic.kaixin001.com.cn/logo/" +s1+ "/" +s2+ "/50_" + uid + "_1.jpg";
		} else if (uid.length() == 8) {
			String s1 = uid.substring(2, 4);
			String s2 = uid.substring(4, 6);
			return "http://pic.kaixin001.com.cn/logo/" +s1+ "/" +s2+ "/50_" + uid + "_1.jpg";
		} else {
			return "http://pic.kaixin001.com.cn/logo/01/01/50_01_01.jpg";
		}
	}
	
	/**
	 * @param args
	 * @throws NetworkException 
	 * @throws ContentException 
	 */
	public static void main(String[] args) throws ContentException, NetworkException {
	}

}
